#include <driver/view/hal.h>
#include <driver/view/view.h>
#include <driver/view/core.h>
#include <driver/view/msg.h>
#include <driver/view/env.h>
#include<driver/view.h>
#include <os/task.h>
#include <os/debug.h>

static task_t *view_thread = NULL;
static int view_thread_exit = 0;

static void ViewThreadEntry(void *arg)
{
    ViewCoreLoop();
}

static void ViewMouseTest()
{
    // KPrint("mouse_test\nx %d y %d\n",view_mouse.x,view_mouse.y);
    // int x=0,y=0;
    // int dx=5,dy=10;
    // for(x=0;x<1024;x++)
    // {
    //     //for(y=0;y=768;y++)
    //     {
    //         ViewSetXY(view_mouse.view,x,y);
    //         y+=dy;
    //     }
    // }
    // y=0;
    // for(x=1024;x>0;x--)
    // {
    //     ViewSetXY(view_mouse.view,x,y);
    //     y-=dy;
    // }
    // ViewSetXY(view_mouse.view,500,800);
}

int ViewCoreInit()
{
    view_thread_exit = 0;
    view_thread = NULL;

    if (ViewScreenInit() < 0)
    {
        KPrint("[view] screen init err\n");
        return -1;
    }

    if (ViewSectionInit() < 0)
    {
        KPrint("[view] section init err\n");
        return -1;
    }
    if (ViewInit() < 0)
    {
        KPrint("[view] view init err\n");
        ViewSectionExit();
        return -1;
    }
    if (ViewMouseInit() < 0)
    {
        KPrint("[view] mouse init err\n");
        ViewExit();
        ViewSectionExit();
        return -1;
    }
    if (ViewKeyboardInit() < 0)
    {
        KPrint("[view] keyboard init err\n");
        ViewMouseExit();
        ViewExit();
        ViewSectionExit();
        return -1;
    }
    if (ViewGlobalMsgInit() < 0)
    {
        KPrint("[view] global msg init err\n");
        ViewKeyboardExit();
        ViewMouseExit();
        ViewExit();
        ViewSectionExit();
        return -1;
    }
    if (ViewEnvInit() < 0)
    {
        KPrint("[view] env init err\n");
        ViewGlobalMsgExit();
        ViewKeyboardExit();
        ViewMouseExit();
        ViewExit();
        ViewSectionExit();
        return -1;
    }
    view_thread = TaskCreate("view", TASK_PRIOR_LEVEL_HIGH, ViewThreadEntry, NULL);
    if (view_thread ==NULL)
    {
        KPrint("[view] view thread create err\n");
        ViewEnvExit();
        ViewGlobalMsgExit();
        ViewKeyboardExit();
        ViewMouseExit();
        ViewExit();
        ViewSectionExit();
        return -1;
    }
    KPrint("[view] init view core ok\n");

    task_t* test_pthread=TaskCreate("mouse_test",TASK_PRIOR_LEVEL_HIGH,ViewMouseTest,NULL);
    if(test_pthread==NULL)
    {
        KPrint("[view] test pthread create err\n");
        return -1;
    }
    return 0;
}

int ViewCoreExit()
{
    view_thread_exit = 1;
    // waitting until thread stop
    while (view_thread_exit)
    {
        TaskYield();
    }
    view_thread = NULL;

    ViewEnvExit();
    ViewGlobalMsgExit();
    ViewKeyboardExit();
    ViewMouseExit();
    ViewExit();
    ViewSectionExit();
    return 0;
}

void ViewCoreLoop()
{
    view_msg_t msg;
    while (!view_thread_exit)
    {
        // get keyboard,mouse input
        ViewMousePoll();
        ViewKeyboardPoll();
        ViewMsgReset(&msg);
        if (ViewGetGlobalMsg(&msg) < 0)
        {
            TaskYield();
            continue;
        }
        // send msg
        if (IsViewMsgVaild(&msg))
        {
            switch (ViewMsgGetId(&msg))
            {
            case VIEW_MSG_KEY_DOWN:
            case VIEW_MSG_KEY_UP:
                ViewDispatchKeycodeMsg(&msg); // send keyboard msg to active layout
                break;
            case VIEW_MSG_MOUSE_MBTN_DOWN:
            case VIEW_MSG_MOUSE_MBTN_UP:
            case VIEW_MSG_MOUSE_MBTN_DCLICK:
            case VIEW_MSG_MOUSE_LBTN_DOWN:
            case VIEW_MSG_MOUSE_LBTN_UP:
            case VIEW_MSG_MOUSE_LBTN_DCLICK:
            case VIEW_MSG_MOUSE_RBTN_DOWN:
            case VIEW_MSG_MOUSE_RBTN_DCLICK:
            case VIWE_MSG_MOUSE_WHEEL_DOWN:
            case VIEW_MSG_MOUSE_WHEEL_UP:
            case VIEW_MSG_MOUSE_WHEEL_LEFT:
            case VIEW_MSG_MOUSE_WHEEL_RIGHT:
            case VIEW_MSG_MOUSE_MOTION:
                ViewDispatchMouseMsg(&msg); // send mouse msg to current layout
                break;
            default:
                ViewDispatchTargeMsg(&msg);
                break;
            }
        }
    }
    view_thread_exit = 0;
    TaskExit(0);
}
